package tools

import (
	"strconv"
)

type Parser struct {
	*ErrCatcher
}

func NewParser() *Parser {
	p := new(Parser)
	p.ErrCatcher = NewErrCatcher()
	p.Strict = true
	return p
}

func (p *Parser) Bool(s string) (ret bool) {
	p.Execute(func() error {
		n, err := strconv.ParseBool(s)
		ret = n
		return err
	})
	return
}

func (p *Parser) Uint64(s string) (ret uint64) {
	p.Execute(func() error {
		n, err := strconv.ParseUint(s, 10, 64)
		ret = n
		return err
	})
	return
}

func (p *Parser) Uint16(s string) (ret uint16) {
	return uint16(p.Uint64(s))
}

func (p *Parser) Uint32(s string) (ret uint32) {
	return uint32(p.Uint64(s))
}

func (p *Parser) Uint(s string) (ret uint) {
	return uint(p.Uint64(s))
}

func (p *Parser) Int64(s string) (ret int64) {
	p.Execute(func() error {
		n, err := strconv.ParseInt(s, 10, 64)
		ret = n
		return err
	})
	return
}
func (p *Parser) Int64Assign(s string, out *int64) {
	if n := p.Int64(s); out != nil && n > 0 {
		*out = n
	}
}

func (p *Parser) Int32(s string) (ret int32) {
	return int32(p.Int64(s))
}
func (p *Parser) Int32Assign(s string, out *int32) {
	if n := p.Int32(s); out != nil && n > 0 {
		*out = n
	}
}

func (p *Parser) Int16(s string) (ret int16) {
	return int16(p.Int64(s))
}

func (p *Parser) Int(s string) (ret int) {
	return int(p.Int64(s))
}
func (p *Parser) IntAssign(s string, out *int) {
	if n := p.Int(s); out != nil && n > 0 {
		*out = n
	}
}
